<template>
  <div id="PageRegister">
    <el-collapse-transition>
      <section v-show="searchshow" class="main-search merge">
        <span class="svg-container">
          <svg-icon icon-class="search" />
        </span>
        <el-input
            v-model.trim="listQuery.pageName"
            :placeholder="`输入${activeTab==='1'?'页面':'接口'}名称搜索`"
            class="search-input"
            @keyup.enter.native="handleFilter"
        />
        <section class="search-btn">
          <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
          <el-button type="primary" @click="handleReset">重置</el-button>
          <span class="search-more" @click="searchshow = !searchshow">
            更多筛选条件
            <i class="el-icon-arrow-down" />
          </span>
        </section>
      </section>
    </el-collapse-transition>
    <el-collapse-transition>
      <section v-show="!searchshow" class="main-search separate">
        <section class="search-inner">
          <el-row>
            <el-col :span="12">
              <section class="search-inner-item">
                <div class="item-label">{{ activeTab==='1'?'页面':'接口' }}名称</div>
                <el-input
                    v-model.trim="search.pageName"
                    style="width: 90%;"
                    class="filter-item"
                    :placeholder="`输入${activeTab==='1'?'页面':'接口'}名称搜索`"
                />
              </section>
            </el-col>
            <el-col v-if="activeTab==='1'" :span="12">
              <section class="search-inner-item">
                <div class="item-label">页面标识</div>
                <el-input
                    v-model.trim="search.pageCode"
                    style="width: 90%;"
                    class="filter-item"
                    placeholder="请输入页面标识搜索"
                />
              </section>
            </el-col>
            <el-col :span="12">
              <section class="search-inner-item">
                <div class="item-label">{{ activeTab==='1'?'页面路由':'接口地址' }}</div>
                <el-input
                    v-model.trim="search.pageRoute"
                    style="width: 90%;"
                    class="filter-item"
                    :placeholder="`请输入${activeTab==='1'?'页面路由':'接口地址'}搜索`"
                />
              </section>
            </el-col>
          </el-row>
        </section>
        <section class="search-btn">
          <el-button type="primary" @click="handleFilter">搜索</el-button>
          <el-button type="primary" @click="handleReset">重置</el-button>
          <span class="search-more" @click="searchshow = !searchshow">
            精简筛选条件
            <i class="el-icon-arrow-up" />
          </span>
        </section>
      </section>
    </el-collapse-transition>
    <div class="course-btn">
      <div class="btn-title">
        <span>页面登记</span>
      </div>
      <div class="btn-btns">
        <el-button
            style="margin-left: 10px;"
            @click="handleCreate"
        >新建
        </el-button>
      </div>
    </div>
    <div class="classify-tab">
      <el-tabs v-model="activeTab" @tab-click="handleTabClick">
        <el-tab-pane
            v-for="item in types"
            :key="item.id"
            :label="item.name"
            :name="item.id"
        />
      </el-tabs>
    </div>
    <div class="course-list">
      <v-table
          v-if="showTable"
          :key="tableKey"
          :list="courseList"
          :tag="tag"
          :list-loading="listLoading"
          :filter-column="filtercolumn"
      >
        <el-table-column fixed="right" label="操作" width="160" align="center">
          <template slot-scope="scope">
            <el-button
                v-if="scope.row.bid!=='0'"
                type="text"
                size="small"
                @click="handleEdit(scope.row)"
            >编辑</el-button>
            <el-button
                type="text"
                size="small"
                @click="handleView(scope.row)"
            >查看</el-button>
            <el-button
                type="text"
                size="small"
                @click="handleCopy(scope.row)"
            >复制</el-button>
            <el-button
                v-if="scope.row.bid!=='0'"
                type="text"
                size="small"
                @click="handleDel(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </v-table>
    </div>
    <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
    />
  </div>
</template>

<script>
import { queryPage, deleteById } from '../api/register'
import vTable from '@/components/Table'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
export default {
  name: 'PageRegister',
  components: { vTable, Pagination },
  data() {
    return {
      searchshow: true,
      listQuery: {
        page: 1,
        limit: 10,
        pageName: ''
      },
      search: {
        pageName: '',
        pageRoute: '',
        pageCode: ''
      },
      listLoading: false,
      courseList: [],
      total: 0,
      tag: 'PageRegister',
      activeTab: '1',
      types: [
        {
          id: '1',
          name: '页面登记'
        },
        {
          id: '2',
          name: '接口登记'
        }
      ],
      tableKey: new Date().getTime(),
      showTable: true
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleReset() {
      this.listQuery.page = 1
      this.listQuery.pageName = ''
      this.search = {
        pageName: '',
        pageRoute: '',
        pageCode: ''
      }
      this.getList()
    },
    /**
     * 选项卡切换
     */
    handleTabClick(tab, event, index) {
      this.listQuery.pageName = ''
      this.listQuery.pageCode = ''
      this.listQuery.pageRoute = ''
      if (tab.name === '1') {
        this.tag = 'PageRegister'
        this.tableKey = new Date().getTime()
      } else {
        this.tag = 'PortRegister'
        this.tableKey = new Date().getTime()
      }
      this.listQuery.page = 1
      this.getList()
    },
    handleDel(row) {
      this.$confirm('此操作将永久删除该页面登记, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({
          lock: true,
          text: '请稍后',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.3)'
        })
        return new Promise((resolve, reject) => {
          deleteById({ id: row.id }).then(() => {
            loading.close()
            this.$message({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          }).catch(error => {
            loading.close()
            reject(error)
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleView(item) {
      this.$router.push({ path: '/pagemanage/register/edit', query: { i: item.id, t: 'look', pt: this.activeTab }})
    },
    handleCopy(item) {
      this.$router.push({ path: '/pagemanage/register/edit', query: { i: item.id, t: 'copy', pt: this.activeTab }})
    },
    handleEdit(item) {
      this.$router.push({ path: '/pagemanage/register/edit', query: { i: item.id, t: 'u', pt: this.activeTab }})
    },
    handleCreate() {
      this.$router.push({ path: '/pagemanage/register/edit', query: { t: 'c', pt: this.activeTab }})
    },
    filtercolumn(scope, item) {
      if (item === 'subjectNum') {
        return `${scope.row.subjectNum || '0'}`
      } else if (item === 'isRecommend') {
        return `${(scope.row.isRecommend === '1' ? '是' : '否')} / ${(scope.row.isDiscuss === 1 ? '是' : '否')}`
      } else {
        return scope.row[item]
      }
    },
    getList(obj) {
      this.listLoading = true
      var query = {
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit,
        pageName: this.listQuery.pageName || '',
        registerType: this.activeTab
      }
      if (!this.searchshow) {
        query.pageName = this.search.pageName || ''
        query.pageCode = this.search.pageCode || ''
        query.pageRoute = this.search.pageRoute || ''
      }
      return new Promise((resolve, reject) => {
        queryPage(query)
            .then((response) => {
              this.courseList = response.data.data.rows
              this.total = response.data.data.total
              this.listLoading = false
            })
            .catch((error) => {
              this.listLoading = false
              reject(error)
            })
      })
    },
    // 搜索
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    // 重置
    resettingBtn() {
      this.listQuery.page = 1
      this.listQuery.pageSize = 10
      this.listQuery.pageName = ''
      this.listQuery.pageCode = ''
      this.listQuery.pageRoute = ''
      for (const key in this.search) {
        this.search[key] = ''
      }
      this.getList()
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" type="text/scss">
$dark_gray: #bfced6;
// $mainColor: #18a5fd;
#PageRegister {
  padding: 10px 20px 10px 10px;
  .main-search {
    background-color: #fff;
    padding: 0 10px;
    .svg-container {
      padding: 0 5px 0 10px;
      color: #bfced6;
      vertical-align: middle;
      display: inline-block;
    }
    .search-input {
      width: 60%;
      position: relative;
      &:after {
        content: '';
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }
      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;
        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }
    .search-btn {
      display: inline-block;
      float: right;
      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }
      .search-more {
        font-size: 14px;
        font-weight: 500;
        @include font-color($mainColor);
        cursor: pointer;
        margin-left: 10px;
      }
    }

    &.merge {
      height: 50px;
      line-height: 50px;
    }

    &.separate {
      .search-inner {
        padding-top: 10px;
        .search-inner-item {
          .item-label {
            height: 30px;
            line-height: 30px;
            font-size: 14px;
            color: #a6a6a6;
            font-weight: 400;
          }
          .el-input {
            position: relative;
            &.el-date-editor input {
              padding-left: 30px;
            }
            .el-input__inner {
              border: none;
              padding: 0;
            }
            &:after {
              content: '';
              height: 1px;
              background-color: #e2e3e8;
              position: absolute;
              left: 0;
              right: 0;
              bottom: 0;
            }
          }
        }
      }
      .search-btn {
        text-align: center;
        display: block;
        float: none;
        height: 56px;
        line-height: 56px;
      }
    }
  }
  .classify-tab {
    width: 100%;
    margin: 10px 0;
    padding: 0 10px;
    background-color: #fff;
    display: flex;
    border-radius: 4px;
    .el-tabs__nav-wrap::after{
      display: none;
    }
    .el-tabs__header {
      margin: 0;
    }
  }
  .course-btn {
    display: flex;
    height: 46px;
    line-height: 46px;
    width: 100%;

    .btn-title {
      flex: 1;
      font-size: 16px;
      font-weight: bold;
      color: rgba(51, 51, 51, 1);
    }
  }

  .course-list {
    .el-image {
      width: 100%;
      height: auto;
      max-height: 140px;
    }
  }

  .pagination-container {
    background: none;
  }

  .classify-tab {
    width: 100%;
    margin: 10px 0;
    padding: 0 10px;
    background-color: #fff;
    border-radius: 4px;

    .el-tabs__nav-next {
      line-height: 40px;
    }

    .el-tabs__nav-wrap::after {
      display: none;
    }

    .el-tabs__header {
      margin: 0;
    }
  }
}
</style>
